package view.render.weapon
{
	
	import enums.ColorLib;
	import enums.main.UISize;
	
	import feathers.controls.Button;
	import feathers.controls.Label;
	import feathers.display.Scale3Image;
	
	import model.vo.weapon.BaseShortWeaponVo;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.events.Event;
	import starling.textures.Texture;
	
	import util.AtlasTextureGenerator;
	import util.FG2FUGenerator;
	import util.WeaponUtil;
	
	import view.event.weapon.BaseWeaponSimpleItemEvent;
	import view.render.BaseScrollerItem;
	
	
	/**
	 * 武器简表item基类
	 * @author £iang
	 * 
	 */	
	public class BaseWeaponSimpleItem extends BaseScrollerItem
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 选中背景 */
		public var selectedBg:Scale3Image;
		/** 攻击力信息 */
		public var attackLabel:Label;
		/** 属性信息 */
		public var secondLabel:Label;
		/** 斩味信息 */
		public var thirdLabel:Label;
		/** 查看路线按钮 */
		public var routeBtn:Button;
		/** 查看详细按钮 */
		public var detailBtn:Button;
		/** 武器id */
		public var weaponId:int;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** 是否选中状态 */
		protected var _isSelected:Boolean = false;

		//-----------------------------
		// setter&getter
		//-----------------------------
		
		/** 是否选中状态 */
		public function get isSelected():Boolean
		{
			return _isSelected;
		}
		
		/**
		 * @private
		 */
		public function set isSelected(value:Boolean):void
		{
			_isSelected = value;
			bgRect.visible = !value;
			selectedBg.visible = value;
		}

		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initializeOther():void
		{
			selectedBg = FG2FUGenerator.drawRoundRectImage2(
				_itemH, 
				ColorLib.ORANGE, UISize.DPI_SCALE, 12, "default"
			);
			selectedBg.width = UISize.MAIN_W;
			selectedBg.height = _itemH * UISize.DPI_SCALE;
			selectedBg.visible = false;
			this.addChildAt( selectedBg, 1 );
			
			super.initializeOther();
			
			attackLabel = new Label();
			attackLabel.textRendererProperties.wordWrap = true;
			scollerContainer.addChild( attackLabel );
			
			secondLabel = new Label();
			secondLabel.textRendererProperties.wordWrap = true;
			secondLabel.textRendererProperties.isHTML = true;
			scollerContainer.addChild( secondLabel );
			
			thirdLabel = new Label();
			thirdLabel.textRendererProperties.wordWrap = true;
			thirdLabel.textRendererProperties.isHTML = true;
			scollerContainer.addChild( thirdLabel );
			
			var iconVec:Vector.<Texture> = AtlasTextureGenerator.getRandomCatTextures(
				"head", 2, false );
			var tmpIcon:Image;
			
			tmpIcon = new Image( iconVec[ 0 ] );
			tmpIcon.scaleX = tmpIcon.scaleY = UISize.DPI_SCALE;
			routeBtn = FG2FUGenerator.getNoBGStyleBtn(
				UISize.MAIN_W * 0.3 / UISize.DPI_SCALE, 50, UISize.DPI_SCALE	
			);
			routeBtn.label = "路线";
			routeBtn.defaultIcon = tmpIcon;
			routeBtn.gap = 0;
			scollerContainer.addChild( routeBtn );
			
			tmpIcon = new Image( iconVec[ 1 ] );
			tmpIcon.scaleX = tmpIcon.scaleY = UISize.DPI_SCALE;
			detailBtn = FG2FUGenerator.getNoBGStyleBtn(
				UISize.MAIN_W * 0.3 / UISize.DPI_SCALE, 50, UISize.DPI_SCALE	
			);
			detailBtn.label = "详细";
			detailBtn.defaultIcon = tmpIcon;
			detailBtn.gap = 0;
			scollerContainer.addChild( detailBtn );
			
			addListener();
			
		}
		
		/**
		 * 调整组件 
		 * 
		 */		
		override protected function draw():void
		{
			var labelY:Number = ( _itemH - 
				43 ) / 2 * UISize.DPI_SCALE;
			var leftPadding:Number = UISize.TITLE_PADDING_LEFT * UISize.DPI_SCALE;
			var firstLabelFullW:Number;
			
			firstLabel.x = leftPadding;
			firstLabelFullW = UISize.MAIN_W * _firstPercent;
			
			attackLabel.y = labelY;
			attackLabel.x = 0;
			attackLabel.width  = UISize.MAIN_W * 0.2;
			
			secondLabel.y = labelY;
			secondLabel.x = UISize.MAIN_W * 0.2;
			secondLabel.width  = UISize.MAIN_W * 0.2;
			
			thirdLabel.y = labelY;
			thirdLabel.x = UISize.MAIN_W * 0.4;
			thirdLabel.width  = UISize.MAIN_W * 0.2;
			
			routeBtn.y = labelY;
			routeBtn.x = UISize.MAIN_W * 0.6;
			
			detailBtn.y = labelY;
			detailBtn.x = UISize.MAIN_W * 0.9;
			
			scollerContainer.x = firstLabelFullW;
			scollerContainer.height = _itemH * UISize.DPI_SCALE;
			scollerContainer.width = UISize.MAIN_W - firstLabelFullW;
			
			this.width = UISize.MAIN_W;
			this.height = _itemH * UISize.DPI_SCALE;
		}
		
		/**
		 * 设置item数据 
		 * @param data 数据
		 * 
		 */		
		public function setData( data:BaseShortWeaponVo ):void
		{
			var nameStr:String = data.name + "(" + data.nickName +")";
			
			weaponId = data.weaponId;
			if ( data.isFinal  )
				firstLabel.text = "■";
			firstLabel.text += nameStr;
			firstLabel.color = WeaponUtil.getRareColor( data.rare );
			attackLabel.text = data.attack.toString();
			secondLabel.text = WeaponUtil.getAttributeLabelText( 
				data.attribute, data.attriNum, data.isAwaken
			);
			thirdLabel.text = WeaponUtil.getSimpleSharpeLabelText( data );
		}
		
		/**
		 * 切换页面 
		 * 
		 */		
		public function shiftPage():void
		{
			if ( scollerContainer.horizontalPageIndex == 0 )
				changeScrollerPage( 1 );
			else
				changeScrollerPage( 0 );
		}
		
		/**
		 * 播放隐藏动画 
		 * @param index item序号
		 * 
		 */		
		override public function playHideAnimate( index:int, offset:int ):void
		{
			var tween:Tween = new Tween( this, 0.4, "easeIn" );
			
			tween.animate( "alpha", 0);
			tween.delay = ( index - offset ) * 0.1;
			Starling.juggler.add( tween );
			
			if ( !_isSelected )
				tween = new Tween( bgRect, 0.4, "easeIn" );
			else
				tween = new Tween( selectedBg, 0.4, "easeIn" );
			
			tween.animate( "width", 0);
			tween.delay = ( index - offset ) * 0.1;
			Starling.juggler.add( tween );
		}
		
		/**
		 * 添加侦听 
		 * 
		 */		
		public function addListener():void
		{
			routeBtn.addEventListener( Event.TRIGGERED, routeTriggeredHandler);
			detailBtn.addEventListener( Event.TRIGGERED, detailTriggeredHandler);
		}
		
		//-----------------------------
		// events
		//-----------------------------
	
		/**
		 * 处理点击路线按钮后派发的事件 
		 * @param event 点击路线按钮后派发的事件 
		 * 
		 */		
		protected function routeTriggeredHandler( event:Event ):void
		{
			var shortWeaponItemEvent:BaseWeaponSimpleItemEvent = 
				new BaseWeaponSimpleItemEvent( 
					BaseWeaponSimpleItemEvent.SEARCH_ROUTE
				);
			
			shortWeaponItemEvent.weaponId = weaponId;
			this.parent.parent.parent.parent.dispatchCommonEvent( shortWeaponItemEvent );
		}
		
		/**
		 * 处理点击详细按钮后派发的事件 
		 * @param event 点击详细按钮后派发的事件 
		 * 
		 */		
		protected function detailTriggeredHandler( event:Event ):void
		{
			var shortWeaponItemEvent:BaseWeaponSimpleItemEvent = 
				new BaseWeaponSimpleItemEvent( 
					BaseWeaponSimpleItemEvent.DETAIL
				);
			
			shortWeaponItemEvent.weaponId = weaponId;
			this.parent.parent.parent.parent.dispatchCommonEvent( shortWeaponItemEvent );
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		protected override function disposeAnythingInContainer():void
		{
			disposeLabels( [
				"attack", "second", "third"
			] );
			
			detailBtn.dispose();
			FG2FUGenerator.deepDisposeButtonComp( detailBtn );
			detailBtn = null;
			
			routeBtn.dispose();
			FG2FUGenerator.deepDisposeButtonComp( routeBtn );
			routeBtn = null;
			
			selectedBg.dispose();
			selectedBg.textures.texture.dispose();		
			selectedBg = null;
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function BaseWeaponSimpleItem()
		{
			super();
			_itemH = UISize.GREAT_SWORD_ITEM_H;
			_firstPercent = 0.4;
			initialize();
		}
	}
}